package sanrenxing.foreMain.dao.impl;

import org.omg.SendingContext.RunTime;
import sanrenxing.foreMain.dao.StudentSelfMsgDao;
import sanrenxing.foreMain.domain.StudentSelfMsg;
import sanrenxing.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Administrator
 * @date 2020/10/3 10:00
 * 编辑者：蒋玲苹
 * 功能：
 */
public class StudentSelfMsgDaoImpl implements StudentSelfMsgDao {
    /**
     * 查找个人信息
     * @param stuId 学员编号
     * @return
     */
    @Override
    public StudentSelfMsg findSelfMsg(String stuId) {
        //声明数据库连接对象
        Connection conn=null;
        //声明预编译语句对象
        PreparedStatement pstmt=null;
        //声明结果集
        ResultSet rs=null;
        //创建对象
        StudentSelfMsg studentSelfMsg=null;
        try {
            //实例化数据库连接对象
            conn= JdbcUtils.getConnection();
            //编写要执行的sql命令语句
            String sql="select stuId,stuName,stuSex,c.className,selfId,stuPhone,stuItb from student s join class c on s.classId=c.classId where s.stuId=?";
            //实例化预编译语句对象
            pstmt=conn.prepareStatement(sql);
            //给sql语句中的占位符赋值
            pstmt.setString(1,stuId);
            //执行预编译语句对象并返回结果集
            rs=pstmt.executeQuery();
            //对结果判断
            if (rs.next()){
                //创建学生信息对象
                studentSelfMsg=new StudentSelfMsg();
                //把结果集中的一行数据赋值给学生信息对象
                studentSelfMsg.setStuId(rs.getString("stuId"));
                studentSelfMsg.setStuName(rs.getString("stuName"));
                studentSelfMsg.setStuSex(rs.getString("stuSex"));
                studentSelfMsg.setclassName(rs.getString("className"));
                studentSelfMsg.setSelfId(rs.getString("selfId"));
                studentSelfMsg.setStuPhone(rs.getString("stuPhone"));
                studentSelfMsg.setStuItb(rs.getString("stuItb"));
                //返回个人信息
                return studentSelfMsg;
            }
        } catch (SQLException e) {
            //运行时异常
            new RuntimeException(e.getMessage(),e);
        }finally{
            //释放资源
            JdbcUtils.close(rs,pstmt,conn);
        }
        return null;
    }
}
